define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wdsxh/single/refund/index' + location.search,
                    table: 'mall_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'real_name', title: __('Real_name'), operate: 'LIKE'},
                        {
                            field: 'goods', title: '商品信息', formatter: function (value, row, index) {
                                var return_result = '';
                                $.each(row.goods,function(i,item){
                                    return_result = return_result + "<img width='40px' height='40px' src = "+item.goods_image+">"+ item.goods_name + "￥" + item.goods_price +"x"+item.goods_num+item.unit+"</br></br>";
                                });
                                return return_result;
                            }
                        },
                        {field: 'pay_price', title: __('Pay_price'), operate:'BETWEEN'},
                        {field: 'mallorderrefund.createtime', title: __('发起退款时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'refund_status', title: __('Refund_status'), searchList: {"0":__('Refund_status 0'),"1":__('Refund_status 1'),"2":__('Refund_status 2'),"3":__('Refund_status 3'),"4":__('Refund_status 4')}, formatter: Table.api.formatter.status},
                        {field: 'mallorderrefund.refund_reason', title: __('退款用户说明'), operate:'BETWEEN'},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('操作'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'apply',
                                    text: __('通过申请'),
                                    title: __('通过申请'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    url: 'wdsxh/mall/refund/apply',
                                    confirm: '确认并同意申请？',
                                    visible:function (row){
                                        if(row.refund_status == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $("#table").bootstrapTable('refresh',{});
                                    },
                                    error: function (data, ret) {
                                        Toastr.error(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'receipt',
                                    text: __('确认收货'),
                                    title: __('确认收货'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    url: 'wdsxh/mall/refund/receipt',
                                    confirm: '确认收到货物？',
                                    visible:function (row){
                                        if(row.refund_status == 2 && row.mallorderrefund.refund_express_no != ''){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $("#table").bootstrapTable('refresh',{});
                                    },
                                    error: function (data, ret) {
                                        Toastr.error(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'agree',
                                    text: __('确认退款'),
                                    title: __('确认退款'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    url: 'wdsxh/mall/refund/refund',
                                    confirm: '确认并同意退款？退款后费用将原路退回',
                                    visible:function (row){
                                        if(row.refund_status == 3){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $("#table").bootstrapTable('refresh',{});
                                    },
                                    error: function (data, ret) {
                                        Toastr.error(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    text:'订单详情',
                                    name: 'goods_details',
                                    title: '订单详情',
                                    classname: 'btn btn-xs btn-primary btn-dialog bg-aqua',
                                    icon: 'fa',
                                    url: 'wdsxh/mall/refund/goods_details',
                                    extend:'data-area=["95%","95%"]',
                                    visible: function (row) {
                                        if(row.refund_status == 3){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // recyclebin: function () {
        //     // 初始化表格参数配置
        //     Table.api.init({
        //         extend: {
        //             'dragsort_url': ''
        //         }
        //     });
        //
        //     var table = $("#table");
        //
        //     // // 初始化表格
        //     // table.bootstrapTable({
        //     //     url: 'mall/refund/recyclebin' + location.search,
        //     //     pk: 'id',
        //     //     sortName: 'id',
        //     //     columns: [
        //     //         [
        //     //             {checkbox: true},
        //     //             {field: 'id', title: __('Id')},
        //     //             {
        //     //                 field: 'deletetime',
        //     //                 title: __('Deletetime'),
        //     //                 operate: 'RANGE',
        //     //                 addclass: 'datetimerange',
        //     //                 formatter: Table.api.formatter.datetime
        //     //             },
        //     //             {
        //     //                 field: 'operate',
        //     //                 width: '130px',
        //     //                 title: __('Operate'),
        //     //                 table: table,
        //     //                 events: Table.api.events.operate,
        //     //                 buttons: [
        //     //                     {
        //     //                         name: 'Restore',
        //     //                         text: __('Restore'),
        //     //                         classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
        //     //                         icon: 'fa fa-rotate-left',
        //     //                         url: 'mall/refund/restore',
        //     //                         refresh: true
        //     //                     },
        //     //                     {
        //     //                         name: 'Destroy',
        //     //                         text: __('Destroy'),
        //     //                         classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
        //     //                         icon: 'fa fa-times',
        //     //                         url: 'mall/refund/destroy',
        //     //                         refresh: true
        //     //                     }
        //     //                 ],
        //     //                 formatter: Table.api.formatter.operate
        //     //             }
        //     //         ]
        //     //     ]
        //     // });
        //
        //     // 为表格绑定事件
        //     Table.api.bindevent(table);
        // },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});